package com.lsh.oms.worker.service.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.base.common.json.JsonUtils;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.core.enums.ReturnStatus;
import com.lsh.oms.core.enums.TaskStatus;
import com.lsh.oms.core.enums.TaskType;
import com.lsh.oms.core.model.order.OrderRoHead;
import com.lsh.oms.core.model.po.OmsTask;
import com.lsh.oms.core.proxy.service.EsProxy;
import com.lsh.oms.core.proxy.service.OfcProxy;
import com.lsh.oms.core.service.other.RoOrderService;
import com.lsh.oms.worker.service.base.OmsSimpleElasticJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 18/2/27
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Slf4j
@Deprecated
public class RoStatusQueryFromOfcJop extends OmsSimpleElasticJob {

    public static String SUCCESS = "0000";

    @Autowired
    private RoOrderService roOrderService;

    @Autowired
    private OfcProxy ofcProxy;

    @Autowired
    private EsProxy esProxy;

    @Override
    protected Integer getFetchSize() {
        return 100;
    }

    @Override
    protected TaskType getTaskType() {
        return TaskType.QUERY_RO_STATUS;
    }

    @Override
    protected List<Integer> dealTaskStatus() {
        List<Integer> taskStatusList = new ArrayList<>();
        taskStatusList.add(TaskStatus.TASK_UN_DEAL.getCode());
        taskStatusList.add(TaskStatus.TASK_DEAL_FAIL.getCode());
        return taskStatusList;
    }

    @Override
    protected int process(OmsTask omsTask) {

        OrderRoHead orderRoHead = this.roOrderService.getRoHeadByReturnOrderId(Long.valueOf(omsTask.getRefCode()), true);
        log.info(orderRoHead.getOrderId() + " 订单ro 查询履约结果 开始");
        CommonResult<Object> commonResult = ofcProxy.queryRoStatus(orderRoHead.getReturnOrderId());
        log.info(orderRoHead.getOrderId() + " 订单ro 查询履约结果 结束, 结果 {}", JSON.toJSONString(commonResult));
        // TODO 判断是否成功
        if (!commonResult.getCode().equals(SUCCESS)) {
            return -1;
        }

        int roStatus = orderRoHead.getStatus();

        Object dataObject = commonResult.getData();
        JSONObject dataJsonObject = (JSONObject) dataObject;
        int orderRostatus = dataJsonObject.getInteger("status");

        OrderRoHead update = new OrderRoHead();
        update.setReturnOrderId(orderRoHead.getReturnOrderId());
        //15-提交中；20-提交成功；25-提交失败；30-返仓完成
        int status = ReturnStatus.Return_Success.getIndex();
        if (orderRostatus == 15) {
            status = ReturnStatus.Submiting_So.getIndex();
        } else if (orderRostatus == 20) {
            status = ReturnStatus.Submit_Success_So.getIndex();
        } else if (orderRostatus == 25) {
            status = ReturnStatus.Submit_Fail_So.getIndex();
        }

        if (roStatus != status) {
            update.setStatus(status);
            update.setActivedAt((int) (System.currentTimeMillis() / 1000));
            this.roOrderService.setSoBillCode(update);

            this.notifyEs(update, "orderRoHead " + orderRoHead.getReturnOrderId());
        }

        if (status == ReturnStatus.Return_Success.getIndex()) {
            return 1;
        } else {
            return -1;
        }
    }

    /**
     * @param orderRoHead
     * @param logSign
     */
    private void notifyEs(OrderRoHead orderRoHead, String logSign) {
        log.info(logSign + "ES返仓修改状态入参入参是 -- " + JsonUtils.obj2Json(orderRoHead));
        esProxy.editIndex(orderRoHead, logSign);
    }

}
